package com.gabriellaet.subtitle
{	
	public class Timestamp
	{
		public const PATTERN:RegExp =/(\d{2}):(\d{2}):(\d{2}),(\d*)?/;
		public var stamp:String;
		public var value:Number = 0.0;
		public var hours:Number = 0.0;
		public var minutes:Number = 0.0;
		public var seconds:Number = 0.0;
		public var milliseconds:Number = 0.0;
		
		public function Timestamp(stamp:String=null)
		{
			if(stamp == null || !stamp.match(PATTERN)){
				throw new Error("Invalid timestamp: "+(stamp || "null"));
			}
			
			var n:Array = PATTERN.exec(stamp).slice(1);
			this.stamp = stamp;
			this.hours = n[0] || 0.0;
			this.minutes = n[1] || 0.0;
			this.seconds = n[2] || 0.0;
			this.milliseconds = n[3] || 0.0;
			this.value = (((hours * 3600) + (minutes * 60) + seconds) * 1000) + milliseconds;
		}
		
		public function toString():String
		{
			return stamp || "";
		}
		
		public function valueOf():Number
		{
			return value;
		}

	}
}